<?php

class AboutController extends SBaseController {

    public $layout = 'main';

    /**
     * @var CActiveRecord the currently loaded data model instance.
     */
    private $_model;

    /**
     * Displays a particular model.
     */
    public function actionView() {
        $this->layout = "detail";
        $this->render('view', array(
            'model' => $this->loadModel(),
        ));
    }

    /**
     * Creates a new model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     */
    public function actionCreate() {
        $model = new About();
        $model->lang = Lvc::langId();
        $this->pageTitle = Yii::t('BackEnd', 'Create') . Yii::t('BackEnd', 'about');
        // Uncomment the following line if AJAX validation is needed
        // $this->performAjaxValidation($model);

        if (isset($_POST['About'])) {
            $model->attributes = $_POST['About'];
            if ($model->validate()) {
                if ($model->insert()) {
                    Yii::app()->user->setFlash('message', Yii::t('Global', 'Create successfully.'));
                    $this->redirect(array('view', 'id' => $model->id));
                }
            }
        }

        $this->layout = "detail";
        $this->render('create', array(
            'model' => $model,
        ));
    }

    /**
     * Updates a particular model.
     * If update is successful, the browser will be redirected to the 'view' page.
     */
    public function actionUpdate() {
        $model = $this->loadModel();

        // Uncomment the following line if AJAX validation is needed
        // $this->performAjaxValidation($model);

        $this->pageTitle = Yii::t('BackEnd', 'Create') . Yii::t('Update', 'about');

        if (isset($_POST['About'])) {
            $model->attributes = $_POST['About'];
            if ($model->validate()) {
                if ($model->update()) {
                    Yii::app()->user->setFlash('message', Yii::t('Global', 'Update successfully.'));
                    $this->redirect(array('view', 'id' => $model->id));
                }
            }
        }

        $this->layout = "detail";
        $this->render('update', array(
            'model' => $model,
        ));
    }

    /**
     * Deletes a particular model.
     * If deletion is successful, the browser will be redirected to the 'admin' page.
     */
    public function actionDelete() {
        if (isset($_POST['select-row']) && is_array($_POST['select-row'])) {
            $ids = $_POST['select-row'];
            $this->deleteByIds($ids);
            $this->redirect(array('admin'));
        } elseif (Yii::app()->request->isPostRequest) {
            $this->deleteByIds(Lvc::getId());
            if (!isset($_GET['ajax']))
                $this->redirect(array('admin'));
        }
        else
            throw new CHttpException(400, 'Invalid request. Please do not repeat this request again.');
    }

    private function deleteByIds($ids = array()) {
        if ($ids) {
            if (Lvc::allowedDelete() === 'true') {
                About::model()->deleteAll('id in (' . implode(',', $ids) . ')');
                Yii::app()->end();
            }
        }
    }

    /**
     * Manages all models.
     */
    public function actionAdmin() {
        $this->pageTitle = Yii::t('BackEnd', 'About manage');
        $model = new About('search');
        $model->lang = Lvc::langId();

        if (isset($_GET['About']))
            $model->attributes = $_GET['About'];

        $this->layout = "list";
        $this->render('admin', array(
            'model' => $model,
        ));
    }

    /**
     * Returns the data model based on the primary key given in the GET variable.
     * If the data model is not found, an HTTP exception will be raised.
     */
    public function loadModel() {
        if ($this->_model === null) {
            if (isset($_GET['id']))
                $this->_model = About::model()->findbyPk($_GET['id']);
            if ($this->_model === null)
                throw new CHttpException(404, 'The requested page does not exist.');
        }
        return $this->_model;
    }

    /**
     * Performs the AJAX validation.
     * @param CModel the model to be validated
     */
    protected function performAjaxValidation($model) {
        if (isset($_POST['ajax']) && $_POST['ajax'] === 'about-form') {
            echo CActiveForm::validate($model);
            Yii::app()->end();
        }
    }

}
